package org.hncors.gnssparse.base;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.hncors.gnssparse.R;
import org.hncors.gnssparse.util.StringUtils;
import org.hncors.gnssparse.util.ToastUtil;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class BaseActivity extends FragmentActivity {

    private LinearLayout base_main;
    private LinearLayout base_top;
    private boolean isRun;
    protected MyApplication application;
    protected boolean isGranter = false;
    protected final int REQUEST_PHONE_PERMISSIONS = 0;
    public TextView topRightText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        application = MyApplication.getInstance();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_base_frame);
        initBaseView();
        //开启UMENG统计
    }

    protected void initBaseView(){
        base_main = (LinearLayout) findViewById(R.id.base_main);
        base_top = (LinearLayout) findViewById(R.id.base_top);
    }

    /**
     * 设置页面主体内容
     */
    protected void appendMainBody(Object object,int pResID) {
        View view = LayoutInflater.from(this).inflate(pResID, null);
        x.view().inject(object,view);
        RelativeLayout.LayoutParams _LayoutParams = new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        base_main.addView(view,_LayoutParams);
    }

    /**
     * 设置页面头部内容
     */
    protected void appendTopBody(int pResID) {
        base_top.setVisibility(View.VISIBLE);
        View view = LayoutInflater.from(this).inflate(pResID,null);
        base_top.addView(view);
        topRightText = (TextView) view.findViewById(R.id.top_right_text);
        setTopLeftListener(this);
    }

    /**
     * 设置页面头部标题
     */
    protected void setTopBarTitle(String pText) {
        TextView tvTitle = (TextView) findViewById(R.id.top_center_text);
        if (tvTitle!=null) {
            tvTitle.setText(pText);
        }
    }
    protected void setTopLeftGone() {
        ImageButton tvTitle = (ImageButton) findViewById(R.id.top_left);
        if (tvTitle!=null) {
            tvTitle.setVisibility(View.GONE);
        }
    }
    /**
     * 弹出提示信息
     */
    public void showMsg(String pMsg) {
        if (StringUtils.isBlank(pMsg)) {
            return;
        }
        ToastUtil.show(this, pMsg);
//        MyToast m = MyToast.makeText(this, pMsg, 2000);
//        m.setGravity(Gravity.BOTTOM, 0, 200);
//        m.show();
    }


    /**
     * 设置返回事件
     */
    public void setTopLeftListener(final Activity activity){
        ImageButton top_left = (ImageButton) findViewById(R.id.top_left);
        top_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    /**
     *  默认监听
     */
    protected void setTopLeftDefultListener(){
        View leftView=findViewById(R.id.top_left);
        if(leftView!=null){
            leftView.setVisibility(View.VISIBLE);
            leftView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finishActivitySlide(BaseActivity.this);
                }
            });
        }
    }

    /**
     *  默认监听并关闭键盘
     */
    protected void setTopLeftClosKeybordListener(final Activity activity){
        View leftView=findViewById(R.id.top_left);
        if(leftView!=null){
            leftView.setVisibility(View.VISIBLE);
            leftView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cliclBack(activity);
                    finishActivitySlide(BaseActivity.this);
                }
            });
        }
    }

    public void cliclBack(Activity activity){
        InputMethodManager m = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();
        if(view != null){
            m.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


    /**
     * 关闭页面 带效果
     */
    public void finishActivitySlide(Activity activity){
        activity.finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }
    public void checkPermission() {
        final List<String> permissionsList = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if ((checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if ((checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            if ((checkSelfPermission(Manifest.permission.CAMERA )!= PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.CAMERA );
            if ((checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION )!= PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION );
            if ((checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION  )!= PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION  );
            if (permissionsList.size() == 0){
//                startRecord();
                isGranter = true;
            }else{
                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                        REQUEST_PHONE_PERMISSIONS);
            }
        }else{
//            startRecord();
            isGranter = true;
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PHONE_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    isGranter = true;
                } else {
                    showMsg("您需要开通权限，并重启应用！");
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

}
